package erps.erps.amq;

import javax.jms.Connection;  
import javax.jms.ConnectionFactory;  
import javax.jms.Destination;  
import javax.jms.MessageConsumer;  
import javax.jms.Session;  
import javax.jms.TextMessage;  
import javax.swing.JOptionPane;

import org.apache.activemq.ActiveMQConnection;  
import org.apache.activemq.ActiveMQConnectionFactory;  

import scau.erps.app.Erps;
import scau.erps.view.SandPanel;
import scau.erps.view.dashboard.DashBoardDialog;
  
public class Reciver extends Thread{  
    private static SandPanel sandPanel;
    public Reciver(SandPanel sandPanel){
    	super();
    	this.sandPanel=sandPanel;
    }
    public static void main(String[] args) {  
        // ConnectionFactory ：连接工厂，JMS 用它创建连接  
        ConnectionFactory connectionFactory;  
        // Connection ：JMS 客户端到JMS Provider 的连接  
        Connection connection = null;  
        // Session： 一个发送或接收消息的线程  
        Session session;  
        // Destination ：消息的目的地;消息发送给谁.  
        Destination destination;  
        // 消费者，消息接收者  
        MessageConsumer consumer;  
        connectionFactory = new ActiveMQConnectionFactory(  
                ActiveMQConnection.DEFAULT_USER,  
                ActiveMQConnection.DEFAULT_PASSWORD,  
                "tcp://localhost:61616");  
        try {  
            // 构造从工厂得到连接对象  
            connection = connectionFactory.createConnection();  
            // 启动  
            connection.start();  
            // 获取操作连接  
            session = connection.createSession(Boolean.FALSE,  
                    Session.AUTO_ACKNOWLEDGE);  
            // 获取session注意参数值xingbo.xu-queue是一个服务器的queue，须在在ActiveMq的console配置  
            destination = session.createQueue("Erps");  
            consumer = session.createConsumer(destination);  
            while (true) {  
                //设置接收者接收消息的时间，为了便于测试，这里谁定为100s  
                TextMessage message = (TextMessage) consumer.receive(0);                 
                if (null != message){
                	String text=message.getText();
                	if (Integer.parseInt(text.substring(1, 2))==Erps.getCurrentTeam().getId()){
                        JOptionPane.showMessageDialog(null, text, "提示", JOptionPane.INFORMATION_MESSAGE);
                        sandPanel.setDashBoardData(1);
                	}
                } else {  
                    break;  
                }  
            }  
        } catch (Exception e) {  
            e.printStackTrace();  
        } finally {  
            try {  
                if (null != connection)  
                    connection.close();  
            } catch (Throwable ignore) {  
            }  
        }  
    }  
    public void run(){
        main(null);
      }
} 
